package com.tianruan.dc.modules.sys.user.security;

import com.alibaba.fastjson.JSONObject;
import com.tianruan.dc.common.utils.response.VResponse;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @PS
 */
@Configuration
public class CustomerAuthenticationEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException {
        //设置response状态码，返回错误信息等

        int status = HttpServletResponse.SC_UNAUTHORIZED;

        response.setContentType("application/json;charset=utf-8");
        response.setStatus(status);
        PrintWriter out = response.getWriter();
        VResponse<Object> unLogin = VResponse.error(status, "未登录");
        out.write(JSONObject.toJSONString(unLogin));
        out.flush();
        out.close();
    }

}